package com.libui.permissions

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.Window
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.OnPermissionInterceptor
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.libui.R
import com.libui.dialog.DialogEx
import com.libui.dialog.DialogType
import java.util.UUID

class Permissions(private val context: Context) {
    private var permissionDialog: Dialog? = null

    fun requestCameraPermission(callback: PermissionCallback) {
        XXPermissions.with(context).permission(Permission.CAMERA)
            .interceptor(object : OnPermissionInterceptor {
                override fun launchPermissionRequest(
                    activity: Activity,
                    allPermissions: MutableList<String>,
                    callback: OnPermissionCallback?
                ) {
                    super.launchPermissionRequest(activity, allPermissions, callback)

                    val contentView = PermissionView(context)
                    contentView.setTitle(context.getString(R.string.permission_camera_title))
                    contentView.setMsg(context.getString(R.string.permission_camera_desc))

                    permissionDialog = Dialog(context)
                    permissionDialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
                    permissionDialog!!.setContentView(contentView)
                    permissionDialog!!.window?.setWindowAnimations(R.style.PermissionAnimations)

                    permissionDialog!!.setCanceledOnTouchOutside(false)
                    permissionDialog!!.window?.setGravity(Gravity.TOP)
                    permissionDialog!!.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                    permissionDialog!!.show()
                }

                override fun finishPermissionRequest(
                    activity: Activity,
                    allPermissions: MutableList<String>,
                    skipRequest: Boolean,
                    callback: OnPermissionCallback?
                ) {
                    super.finishPermissionRequest(
                        activity, allPermissions, skipRequest, callback
                    )

                    permissionDialog?.dismiss()
                    permissionDialog = null
                }
            }).request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    callback.onGranted(permissions, allGranted)
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    callback.onDenied(permissions, doNotAskAgain)

                    DialogEx.DialogBuilder(
                        DialogType.ALERT,
                        UUID.randomUUID().toString(),
                        context.getString(R.string.permission_camera_error)
                    ).build(context)?.show()
                }
            })
    }
}